package com.ymm.lulutong.repo;

import com.ymm.lulutong.utils.Result;
import com.ymm.system.api.dto.ShipperOrderBriefResponse;
import com.ymm.system.api.dto.ShipperOrderDetailResponse;
import com.ymm.system.api.facade.SystemShipperOrderService;
import com.ymm.system.api.utils.OrderStatusEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemShipperOrderServiceRepo {
        @Reference(version = "server")
//    @Reference
            SystemShipperOrderService systemShipperOrderService;

    public Result<Boolean> cancelOrderWithPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) throws Exception {
        return systemResult2ShipperResult(systemShipperOrderService.cancelOrderWithPullOffCargo(orderId, shipperId, orderCancelReason, orderCancelDescription));
    }

    public Result<Boolean> cancelOrderWithoutPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) throws Exception {
        return systemResult2ShipperResult(systemShipperOrderService.cancelOrderWithoutPullOffCargo(orderId, shipperId, orderCancelReason, orderCancelDescription));
    }

    public Result<Boolean> confirmReceipt(Long orderId, Long shipperId) throws Exception {
        return systemResult2ShipperResult(systemShipperOrderService.confirmReceipt(orderId, shipperId));

    }

    public Result<ShipperOrderDetailResponse> getOrderDetailByOrderId(Long orderId) {
        return systemResult2ShipperResult(systemShipperOrderService.getOrderDetailByOrderId(orderId));
    }

    public Result<List<ShipperOrderBriefResponse>> getOngoingOrderByShipperId(Long shipperId) {
        return systemResult2ShipperResult(systemShipperOrderService.listOngoingOrdersByShipperId(shipperId, OrderStatusEnum.ONGOING.getCode()));
    }

    public Result<List<ShipperOrderBriefResponse>> getCompletedOrderByShipperId(Long shipperId) {
        return systemResult2ShipperResult(systemShipperOrderService.listCompletedOrdersByShipperId(shipperId, OrderStatusEnum.COMPLETED.getCode()));
    }

    public Result<List<ShipperOrderBriefResponse>> getCancelledOrderByShipperId(Long shipperId) {
        return systemResult2ShipperResult(systemShipperOrderService.listCancelledOrdersByShipperId(shipperId, OrderStatusEnum.CANCELLED.getCode()));
    }

    public Result<String> getDriverPhoneNumber(Long orderId) {
        return systemResult2ShipperResult(systemShipperOrderService.getDriverPhoneNumber(orderId));
    }


    private Result systemResult2ShipperResult(com.ymm.system.api.utils.Result result) {
        if (result.isSuccess()) {
            return Result.success(result.getData());
        }
        return Result.fail(result.getData(), result.getCode(), result.getMessage());
    }
}
